package com.dulayev.chrono;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

public class MainActivity extends Activity {

	protected TimeLine timeLine = new TimeLine(this);
	private GridView grid;
	private CustomAdapter gridAdapter;
	
	public class CustomAdapter extends BaseAdapter {

		String[] tasks = null;
		String activeTask = null;

		private void recreateButtons() {
			tasks = timeLine.getTasks();
			activeTask = timeLine.getActiveTask();
			grid.invalidateViews();
		}
		
		@Override
		public int getCount() {
			return (tasks != null) ? (tasks.length + 1) : 0;
		}

		@Override
		public Object getItem(int position) {
			return (tasks != null) ? tasks[position] : null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			if(tasks == null)
				return null;
			
			Button button = new Button(MainActivity.this);
			
			if(position < tasks.length) {
				button.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						Button button = (Button)v;
						timeLine.startTask(button.getText().toString());
						recreateButtons();
					}
				});
				button.setText(tasks[position]);
				if(tasks[position].equals(activeTask))
					button.setBackgroundColor(Color.YELLOW);
			}
			else {
				button.setOnClickListener(new OnAddButtonClickListener());
				button.setText("+");
			}
			
			return button;
		}
		
		public class OnAddButtonClickListener implements OnClickListener {

			@Override
			public void onClick(View view) {
				
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
	            final EditText editText = new EditText(MainActivity.this);
				
				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					    String taskName = editText.getText().toString();
					    timeLine.addTask(taskName);
					    recreateButtons();
					}});


					builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog, int which) {
					         dialog.dismiss();
					         dialog.cancel();
					    }
					});
				
				final AlertDialog dialog = builder.create();
	            dialog.setTitle("New Task Name");

	            dialog.setView(editText);
	            dialog.show();
			}

		}

	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		grid = (GridView)findViewById(R.id.grid);
		gridAdapter = new CustomAdapter();
		grid.setAdapter(gridAdapter);
		
		gridAdapter.recreateButtons();
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch(item.getItemId()) {
		case R.id.day_statistics:
			startActivity(new Intent(this, ListActivity.class));
			break;
		}
		return true;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
